﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.DirectX.Direct3D;
using System.Threading;

namespace MediaCenter.Controls
{
    public class LCARS_Elbo : LCARS_Control
    {
        private int _HorizontalHeight;
        private int _VerticalWidth;
        private ElboType _TextAligment;
        private Device D3X_Dev;
        private Microsoft.DirectX.Direct3D.Font dx_text;
        private ElboType _elboType;

        public ElboType ElboType
        {
            get
            {
                return _elboType;
            }
            set
            {
                _elboType = value;
            }
        }

        public ElboType TextAligment
        {
            get
            {
                return _TextAligment;
            }
            set
            {
                _TextAligment = value;
            }
        }

        public int HorizontalHeight
        {
            get
            {
                return _HorizontalHeight;
            }
            set
            {
                _HorizontalHeight = value;
            }
        }

        public int VerticalWidth
        {
            get
            {
                return _VerticalWidth;
            }
            set
            {
                _VerticalWidth = value;
            }
        }


        public LCARS_Elbo()
        {
            _HorizontalHeight = 20;
            _VerticalWidth = 100;
            _elboType = ElboType.oben_links;
            _TextAligment = ElboType.oben_links;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (this.DesignMode || this.PartmentForm.RenderMode == RenderMode.GDI)
            {
                switch (_elboType)
                {
                    case ElboType.oben_links:
                        gdi_draw_Elbo_oben_links(e.Graphics);
                        break;
                    case ElboType.oben_rechts:
                        gdi_draw_Elbo_oben_rechts(e.Graphics);
                        break;
                    case ElboType.unten_links:
                        gdi_draw_Elbo_unten_links(e.Graphics);
                        break;
                    case ElboType.unten_rechts:
                        gdi_draw_Elbo_unten_rechts(e.Graphics);
                        break;
                }
            }
        }

        private void gdi_draw_Elbo_unten_rechts(Graphics g)
        {
            //4eck oben
            int Kreisradius = this.HorizontalHeight * 2;
            gdi_draw_4Eck(g,new Rectangle(this.Width - this.VerticalWidth, Kreisradius, this.VerticalWidth, this.Height - Kreisradius));
            //4eck_links unten;
            gdi_draw_4Eck(g,new Rectangle(0,
                0,
                this.Width - Kreisradius, HorizontalHeight));
            //4eck_Lücke
            gdi_draw_4Eck(g,new Rectangle(this.Width - this.VerticalWidth, HorizontalHeight, this.VerticalWidth - Kreisradius, this.HorizontalHeight));
            //kreis unten rechts
            gdi_draw_Kreis(g,0, 90, 30, Kreisradius, new Point(this.Width - Kreisradius, Kreisradius));
            //kreisNegativ unten links
            gdi_draw_Kreis_negativ(g,0, 90, 30, Kreisradius / 2, new Point(this.Width - this.VerticalWidth, this.HorizontalHeight));
            
            int txtbreite = TextRenderer.MeasureText(this.Text, this.Font).Width;
            int txthöhe = TextRenderer.MeasureText(this.Text, this.Font).Height;
            Brush strB = new SolidBrush(this.ForeColor);
            switch (_TextAligment)
            {
                case ElboType.oben_links:
                    g.DrawString(this.Text, this.Font, strB, 5, 5);
                    break;
                case ElboType.oben_rechts:
                    g.DrawString(this.Text, this.Font, strB, this.Width - this.VerticalWidth - txtbreite, 5);
                    break;
                case ElboType.unten_links:
                    g.DrawString(this.Text, this.Font, strB, this.Width - this.VerticalWidth + 5, this.Height - txthöhe - 5);
                    break;
                case ElboType.unten_rechts:
                    g.DrawString(this.Text, this.Font, strB, this.Width - txtbreite - 5, this.Height - txthöhe - 5);
                    break;
            }
        }

        private void gdi_draw_Elbo_unten_links(Graphics g)
        {
            //4eck oben
            int Kreisradius = this.HorizontalHeight * 2;
            gdi_draw_4Eck(g, new Rectangle(0, Kreisradius, this.VerticalWidth, this.Height - Kreisradius));
            //4eck_links unten;
            gdi_draw_4Eck(g, new Rectangle(Kreisradius,
                0,
                this.Width - Kreisradius, HorizontalHeight));
            //4eck_Lücke
            gdi_draw_4Eck(g, new Rectangle(Kreisradius, HorizontalHeight, this.VerticalWidth - Kreisradius, this.HorizontalHeight));
            //kreis unten rechts
            gdi_draw_Kreis(g, 90, 180, 30, Kreisradius, new Point(Kreisradius, Kreisradius));
            //kreisNegativ unten links
            gdi_draw_Kreis_negativ(g, 90, 180, 30, Kreisradius / 2, new Point(this.VerticalWidth, this.HorizontalHeight));
            
            int txtbreite = TextRenderer.MeasureText(this.Text, this.Font).Width;
            int txthöhe = TextRenderer.MeasureText(this.Text, this.Font).Height;
            Brush strB = new SolidBrush(this.ForeColor); 
            switch (_TextAligment)
            {
                case ElboType.oben_links:
                    g.DrawString(this.Text, this.Font, strB, this.VerticalWidth, 5);
                    break;
                case ElboType.oben_rechts:
                    g.DrawString(this.Text, this.Font, strB, this.Width - txtbreite - 5, 5);
                    break;
                case ElboType.unten_links:
                    g.DrawString(this.Text, this.Font, strB, 5, this.Height - txthöhe - 5);
                    break;
                case ElboType.unten_rechts:
                    g.DrawString(this.Text, this.Font, strB, this.VerticalWidth - txtbreite - 5, this.Height - txthöhe - 5);
                    break;
            }
        }

        private void gdi_draw_Kreis_negativ(Graphics g, int startgrad, int endgrad, int teile, int radius, Point Possition)
        {
            Brush b = new SolidBrush(this.BackColor);

            Point kreismitte = getPoint_K_M(startgrad + (endgrad - startgrad) / 2, radius, Possition);

            double grad = System.Convert.ToDouble(startgrad);
            double addgrad_t = (endgrad - startgrad) / teile * (1 - 1);
            double angle_t = Math.PI * (grad + (addgrad_t)) / 180.0;
            double sinus_t = Math.Sin(angle_t) * -1;
            double cosinus_t = Math.Cos(angle_t);
            int x_t = System.Convert.ToInt32(kreismitte.X + radius * cosinus_t);
            int y_t = System.Convert.ToInt32(kreismitte.Y + radius * sinus_t);

            Point lastpoint = new Point(x_t, y_t);


            for (int i = 2; i < teile + 2; i++)
            {
                //int pos = teile + 2 - i;
                int pos = i;
                double addgrad = (endgrad - startgrad) / teile * (i - 1);
                double angle = Math.PI * (grad + (addgrad)) / 180.0;
                double sinus = Math.Sin(angle) * -1;
                double cosinus = Math.Cos(angle);
                int x = System.Convert.ToInt32(kreismitte.X + radius * cosinus);
                int y = System.Convert.ToInt32(kreismitte.Y + radius * sinus);

                Point[] p = new Point[3];
                p[0] = Possition;
                p[1] = lastpoint;
                p[2] = new Point(x, y);
                g.FillPolygon(b, p);
                lastpoint = p[2];

            }
        }

        private void gdi_draw_Kreis(Graphics g, int startgrad, int endgrad, int teile, int radius, Point Possition)
        {
            Brush b = new SolidBrush(this.BackColor);
            double grad = System.Convert.ToDouble(startgrad);
            double addgrad_t = (endgrad - startgrad) / teile * (1 - 1);
            double angle_t = Math.PI * (grad + (addgrad_t)) / 180.0;
            double sinus_t = Math.Sin(angle_t) * -1;
            double cosinus_t = Math.Cos(angle_t);
            int x_t = System.Convert.ToInt32(Possition.X + radius * cosinus_t);
            int y_t = System.Convert.ToInt32(Possition.Y + radius * sinus_t);

            Point lastpoint = new Point(x_t, y_t);

            for (int i = 2; i < teile + 2; i++)
            {
                int pos = teile + 2 - i;
                //int pos = i;
                double addgrad = (endgrad - startgrad) / teile * (i - 1);
                double angle = Math.PI * (grad + (addgrad)) / 180.0;
                double sinus = Math.Sin(angle) * -1;
                double cosinus = Math.Cos(angle);
                int x = System.Convert.ToInt32(Possition.X + radius * cosinus);
                int y = System.Convert.ToInt32(Possition.Y + radius * sinus);

                Point[] p = new Point[3];
                p[0] = Possition;
                p[1] = lastpoint;
                p[2] = new Point(x, y);
                g.FillPolygon(b, p);
                lastpoint = p[2];
            }
        }

        private void gdi_draw_4Eck(Graphics g, Rectangle data)
        {
            Brush b = new SolidBrush(this.BackColor);
            g.FillRectangle(b, data);
        }



        private void gdi_draw_Elbo_oben_rechts(Graphics g)
        {
            //4eck oben
            int Kreisradius = this.HorizontalHeight * 2;
            gdi_draw_4Eck(g,new Rectangle(this.Width - this.VerticalWidth, 0, this.VerticalWidth, this.Height - Kreisradius));
            //4eck_links unten;
            gdi_draw_4Eck(g,new Rectangle(0,
                this.Height - this.HorizontalHeight,
                this.Width - Kreisradius, HorizontalHeight));
            //4eck_Lücke
            gdi_draw_4Eck(g,new Rectangle( this.Width - this.VerticalWidth, this.Height - Kreisradius, this.VerticalWidth - Kreisradius, this.HorizontalHeight));
            //kreis unten rechts
            gdi_draw_Kreis(g,270, 360, 30, Kreisradius, new Point(this.Width - Kreisradius, this.Height - Kreisradius));
            //kreisNegativ unten links
            gdi_draw_Kreis_negativ(g,270, 360, 30, Kreisradius / 2, new Point(this.Width - this.VerticalWidth, this.Height - this.HorizontalHeight));
            
            int txtbreite = TextRenderer.MeasureText(this.Text, this.Font).Width;
            int txthöhe = TextRenderer.MeasureText(this.Text, this.Font).Height;
            Brush strB = new SolidBrush(this.ForeColor);
            switch (_TextAligment)
            {
                case ElboType.oben_links:
                    g.DrawString(this.Text, this.Font, strB, this.Width - this.VerticalWidth + 5, 5);
                    break;
                case ElboType.oben_rechts:
                    g.DrawString(this.Text, this.Font, strB, this.Width - txtbreite - 5, 5);
                    break;
                case ElboType.unten_links:
                    g.DrawString(this.Text, this.Font, strB, 5, this.Height - txthöhe - 5);
                    break;
                case ElboType.unten_rechts:
                    g.DrawString(this.Text, this.Font, strB, this.Width - this.VerticalWidth - txtbreite - 5, this.Height - txthöhe - 5);
                    break;
            }
        }

        private void gdi_draw_Elbo_oben_links(Graphics g)
        {
            //4eck oben
            int Kreisradius = this.HorizontalHeight * 2;
            gdi_draw_4Eck(g,new Rectangle(0, 0, this.VerticalWidth, this.Height - Kreisradius));
            //4eck_links unten;
            gdi_draw_4Eck(g,new Rectangle(Kreisradius,
                this.Height - this.HorizontalHeight,
                this.Width - Kreisradius, HorizontalHeight));
            //4eck_Lücke
            gdi_draw_4Eck(g,new Rectangle( Kreisradius, this.Height - Kreisradius, this.VerticalWidth - Kreisradius, this.HorizontalHeight));
            //kreis unten rechts
            gdi_draw_Kreis(g,180, 270, 30, Kreisradius, new Point(Kreisradius, this.Height - Kreisradius));
            //kreisNegativ unten links
            gdi_draw_Kreis_negativ(g,180, 270, 30, Kreisradius / 2, new Point(this.VerticalWidth, this.Height - this.HorizontalHeight));
            
            int txtbreite = TextRenderer.MeasureText(this.Text, this.Font).Width;
            int txthöhe = TextRenderer.MeasureText(this.Text, this.Font).Height;
            Brush strB = new SolidBrush(this.ForeColor); 
            switch (_TextAligment)
            {
                case ElboType.oben_links:
                    g.DrawString(this.Text, this.Font, strB, 5, 5);
                    break;
                case ElboType.oben_rechts:
                    g.DrawString(this.Text, this.Font, strB, this.VerticalWidth - txtbreite - 5, 5);
                    break;
                case ElboType.unten_links:
                    g.DrawString(this.Text, this.Font, strB,this.VerticalWidth , this.Height - txthöhe - 5);
                    break;
                case ElboType.unten_rechts:
                    g.DrawString(this.Text, this.Font, strB, this.Width - txtbreite - 5, this.Height - txthöhe - 5);
                    break;
            }
        }
        public override void Render()
        {
            base.Render();
            if (!this.DesignMode)
            {
                D3X_Dev = this.PartmentForm.D3X_Dev;
                dx_text = new Microsoft.DirectX.Direct3D.Font(D3X_Dev, this.Font);

                switch (_elboType)
                {
                    case ElboType.oben_links:
                        draw_Elbo_oben_links();
                        break;
                    case ElboType.oben_rechts:
                        draw_Elbo_oben_rechts();
                        break;
                    case ElboType.unten_links:
                        draw_Elbo_unten_links();
                        break;
                    case ElboType.unten_rechts:
                        draw_Elbo_unten_rechts();
                        break;
                }
            }
            dx_text = null;
        }

        private void draw_Elbo_unten_links()
        {
            //4eck oben
            int Kreisradius = this.HorizontalHeight * 2;
            draw_4Eck(new Rectangle(this.Left, this.Top + Kreisradius, this.VerticalWidth, this.Height - Kreisradius));
            //4eck_links unten;
            draw_4Eck(new Rectangle(this.Left + Kreisradius,
                this.Top,
                this.Width - Kreisradius, HorizontalHeight));
            //4eck_Lücke
            draw_4Eck(new Rectangle(this.Left + Kreisradius, this.Top + HorizontalHeight, this.VerticalWidth - Kreisradius, this.HorizontalHeight));
            //kreis unten rechts
            draw_Kreis(90, 180, 30, Kreisradius, new Point(this.Left + Kreisradius, this.Top + Kreisradius));
            //kreisNegativ unten links
            draw_Kreis_negativ(90, 180, 30, Kreisradius / 2, new Point(this.Left + this.VerticalWidth, this.Top + this.HorizontalHeight));
            int txtbreite = TextRenderer.MeasureText(this.Text, this.Font).Width;
            int txthöhe = TextRenderer.MeasureText(this.Text, this.Font).Height;
            switch (_TextAligment)
            {
                case ElboType.oben_links:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
                case ElboType.oben_rechts:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
                case ElboType.unten_links:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
                case ElboType.unten_rechts:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
            }
        }

        private void draw_Elbo_oben_rechts()
        {
            //4eck oben
            int Kreisradius = this.HorizontalHeight * 2;
            draw_4Eck(new Rectangle(this.Left + this.Width - this.VerticalWidth, this.Top, this.VerticalWidth, this.Height - Kreisradius));
            //4eck_links unten;
            draw_4Eck(new Rectangle(this.Left,
                this.Top + this.Height - this.HorizontalHeight,
                this.Width - Kreisradius, HorizontalHeight));
            //4eck_Lücke
            draw_4Eck(new Rectangle(this.Left + this.Width - this.VerticalWidth, this.Top + this.Height - Kreisradius, this.VerticalWidth - Kreisradius, this.HorizontalHeight));
            //kreis unten rechts
            draw_Kreis(270, 360, 30, Kreisradius, new Point(this.Left + this.Width - Kreisradius, this.Top + this.Height - Kreisradius));
            //kreisNegativ unten links
            draw_Kreis_negativ(270, 360, 30, Kreisradius / 2, new Point(this.Left + this.Width - this.VerticalWidth, this.Top + this.Height - this.HorizontalHeight));
            int txtbreite = TextRenderer.MeasureText(this.Text, this.Font).Width;
            int txthöhe = TextRenderer.MeasureText(this.Text, this.Font).Height;
            switch (_TextAligment)
            {
                case ElboType.oben_links:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
                case ElboType.oben_rechts:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
                case ElboType.unten_links:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
                case ElboType.unten_rechts:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
            }
        }
        
        private void draw_Elbo_unten_rechts()
        {
            //4eck oben
            int Kreisradius = this.HorizontalHeight * 2;
            draw_4Eck(new Rectangle(this.Left + this.Width - this.VerticalWidth, this.Top + Kreisradius, this.VerticalWidth, this.Height - Kreisradius));
            //4eck_links unten;
            draw_4Eck(new Rectangle(this.Left,
                this.Top,
                this.Width - Kreisradius, HorizontalHeight));
            //4eck_Lücke
            draw_4Eck(new Rectangle(this.Left + this.Width - this.VerticalWidth, this.Top + HorizontalHeight, this.VerticalWidth - Kreisradius, this.HorizontalHeight));
            //kreis unten rechts
            draw_Kreis(0, 90, 30, Kreisradius, new Point(this.Left + this.Width - Kreisradius, this.Top + Kreisradius));
            //kreisNegativ unten links
            draw_Kreis_negativ(0, 90, 30, Kreisradius / 2, new Point(this.Left + this.Width - this.VerticalWidth, this.Top + this.HorizontalHeight));
            int txtbreite = TextRenderer.MeasureText(this.Text, this.Font).Width;
            int txthöhe = TextRenderer.MeasureText(this.Text, this.Font).Height;
            switch (_TextAligment)
            {
                case ElboType.oben_links:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
                case ElboType.oben_rechts:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
                case ElboType.unten_links:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
                case ElboType.unten_rechts:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
            }
        }

        private void draw_Elbo_oben_links()
        {
            //4eck oben
            int Kreisradius = this.HorizontalHeight * 2;
            draw_4Eck(new Rectangle(this.Left,this.Top,this.VerticalWidth,this.Height-Kreisradius));
            //4eck_links unten;
            draw_4Eck(new Rectangle(this.Left + Kreisradius,
                this.Top+this.Height-this.HorizontalHeight,
                this.Width-Kreisradius,HorizontalHeight));
            //4eck_Lücke
            draw_4Eck(new Rectangle(this.Left + Kreisradius, this.Top + this.Height - Kreisradius, this.VerticalWidth - Kreisradius, this.HorizontalHeight));
            //kreis unten rechts
            draw_Kreis(180, 270, 30, Kreisradius, new Point(this.Left + Kreisradius, this.Top + this.Height - Kreisradius));
            //kreisNegativ unten links
            draw_Kreis_negativ(180, 270, 30, Kreisradius/2, new Point(this.Left+this.VerticalWidth, this.Top+this.Height - this.HorizontalHeight));
            int txtbreite = TextRenderer.MeasureText(this.Text, this.Font).Width;
            int txthöhe = TextRenderer.MeasureText(this.Text, this.Font).Height;
            switch (_TextAligment)
            {
                case ElboType.oben_links:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
                case ElboType.oben_rechts:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
                case ElboType.unten_links:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
                case ElboType.unten_rechts:
                    dx_text.DrawText(null, this.Text, new Point(this.Left + this.Width - VerticalWidth + 5, this.Top + this.Height - txthöhe - 5), Color.Black);
                    break;
            }
        }

        private Point getPoint_K_M(int grad, int radius, Point Possition)
        {
            double _radius = System.Convert.ToDouble(radius);
            double _grad = System.Convert.ToDouble(grad % 360);

            double _xm_tan = 0;
            double _ym_tan = 0;
            if (grad != 90 && grad != 270)
            {
                double angle = Math.PI * (grad) / 180.0;
                double tan = Math.Tan(angle);
                _xm_tan = _radius / tan;
                _ym_tan = _radius * tan;
            }

            double xm = 0;
            double ym = 0;
            if (grad < 46)
            {
                xm = Possition.X - radius;
                ym = Possition.Y + _ym_tan;
            }
            else if (grad < 136)
            {
                xm = Possition.X - _xm_tan;
                ym = Possition.Y + radius;
            }
            else if (grad < 225)
            {
                xm = Possition.X + radius;
                ym = Possition.Y - _ym_tan;
            }
            else if (grad < 315)
            {
                xm = Possition.X + _xm_tan;
                ym = Possition.Y - radius;
            }
            else if (grad < 361)
            {
                xm = Possition.X - radius;
                ym = Possition.Y + _ym_tan;
            }

            return new Point(System.Convert.ToInt32(xm), System.Convert.ToInt32(ym));
        }

        private void draw_Kreis_negativ(int startgrad, int endgrad, int teile, int radius, Point Possition)
        {
            CustomVertex.TransformedColored[] _3Eck = new CustomVertex.TransformedColored[teile + 2];
            _3Eck[0].Position = new Microsoft.DirectX.Vector4(Possition.X, Possition.Y, 0, 2.0f); ;
            _3Eck[0].Color = this.BackColor.ToArgb();

            Point kreismitte = getPoint_K_M(startgrad + (endgrad-startgrad)/2,radius,Possition);

            double grad = System.Convert.ToDouble(startgrad);

            for (int i = 1; i < teile + 2; i++)
            {
                //int pos = teile + 2 - i;
                int pos = i;
                double addgrad = (endgrad - startgrad) / teile * (i-1);
                double angle = Math.PI * (grad + (addgrad)) / 180.0;
                double sinus = Math.Sin(angle) * -1;
                double cosinus = Math.Cos(angle);
                int x = System.Convert.ToInt32(kreismitte.X + radius * cosinus);
                int y = System.Convert.ToInt32(kreismitte.Y + radius * sinus);
                _3Eck[pos].Position = new Microsoft.DirectX.Vector4(x, y, 0, 2.0f); ;
                _3Eck[pos].Color = this.BackColor.ToArgb();
            }

            this.D3X_Dev.VertexFormat = CustomVertex.TransformedColored.Format;
            this.D3X_Dev.DrawUserPrimitives(PrimitiveType.TriangleFan, teile, _3Eck);
        }

        private void draw_4Eck(Rectangle data)
        {
            
            CustomVertex.TransformedColored[] _3Eck = new CustomVertex.TransformedColored[3];
            _3Eck[0].Position = new Microsoft.DirectX.Vector4(data.X, data.Y, 0, 2.0f); ;
            _3Eck[1].Position = new Microsoft.DirectX.Vector4(data.X + data.Width, data.Y, 0, 2.0f); ;
            _3Eck[2].Position = new Microsoft.DirectX.Vector4(data.X, data.Y + data.Height, 0, 2.0f); ;


            _3Eck[0].Color = this.BackColor.ToArgb();
            _3Eck[1].Color = this.BackColor.ToArgb();
            _3Eck[2].Color = this.BackColor.ToArgb();

            this.D3X_Dev.VertexFormat = CustomVertex.TransformedColored.Format;
            this.D3X_Dev.DrawUserPrimitives(PrimitiveType.TriangleList, 1, _3Eck);

            _3Eck = new CustomVertex.TransformedColored[3];
            _3Eck[0].Position = new Microsoft.DirectX.Vector4(data.X, data.Y + data.Height, 0, 2.0f); ;
            _3Eck[1].Position = new Microsoft.DirectX.Vector4(data.X + data.Width, data.Y, 0, 2.0f); ;
            _3Eck[2].Position = new Microsoft.DirectX.Vector4(data.X + data.Width, data.Y + data.Height, 0, 2.0f); ;


            _3Eck[0].Color = this.BackColor.ToArgb();
            _3Eck[1].Color = this.BackColor.ToArgb();
            _3Eck[2].Color = this.BackColor.ToArgb();

            this.D3X_Dev.VertexFormat = CustomVertex.TransformedColored.Format;
            this.D3X_Dev.DrawUserPrimitives(PrimitiveType.TriangleList, 1, _3Eck);
        }

        private void draw_Kreis(int startgrad, int endgrad, int teile, int radius, Point Possition)
        {
            CustomVertex.TransformedColored[] _3Eck = new CustomVertex.TransformedColored[teile + 2];
            _3Eck[0].Position = new Microsoft.DirectX.Vector4(Possition.X, Possition.Y, 0, 2.0f); ;
            _3Eck[0].Color = this.BackColor.ToArgb();

            double grad = System.Convert.ToDouble(startgrad);

            for (int i = 1; i < teile + 2; i++)
            {
                int pos = teile + 2 - i;
                //int pos = i;
                double addgrad = (endgrad - startgrad) / teile * (i - 1);
                double angle = Math.PI * (grad + (addgrad)) / 180.0; 
                double sinus = Math.Sin(angle) * -1;
                double cosinus = Math.Cos(angle);
                int x = System.Convert.ToInt32(Possition.X + radius * cosinus);
                int y = System.Convert.ToInt32(Possition.Y + radius * sinus);
                _3Eck[pos].Position = new Microsoft.DirectX.Vector4(x, y, 0, 2.0f); ;
                _3Eck[pos].Color = this.BackColor.ToArgb();
            }

            this.D3X_Dev.VertexFormat = CustomVertex.TransformedColored.Format;
            this.D3X_Dev.DrawUserPrimitives(PrimitiveType.TriangleFan, teile, _3Eck);
        }

    }
}
